﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using log4net;
using log4net.Config;
using System.IO;
using System.Net;
using System.Configuration;

namespace AgileNYCWebService
{
    public static class Enums
    {
        internal enum AuthorizationType
        {
            Email = 0,
            Facebook = 1,
            GooglePlus = 2,
            LinkedIn = 3
        }

        internal enum DeviceOs
        {
            Ios = 0,
            Android = 1,
            WindowsPhone = 2
        }

        internal enum MobileWorkingSystem
        {
            CDMA = 1,
            GSM = 2
        }


        public enum UserStatus
        {
            [Description("Waiting")]
            Waiting,
            [Description("Confirm")]
            Confirm,
            [Description("Cancel")]
            Cancel,
            [Description("Already Added")]
            AlreadyAdded,
            [Description("Unknown User")]
            UnknownUser,
            [Description("Cannot Add Yourself")]
            CannotAddYourself
        }

        public static string GetDescription<T>(this T enumerationValue) where T : struct
        {
            var type = enumerationValue.GetType();
            if (!type.IsEnum) throw new ArgumentException("EnumerationValue must be of Enum type", "enumerationValue");
            var str = enumerationValue.ToString();
            var memberInfo = type.GetMember(str);
            if (memberInfo != null && memberInfo.Length > 0)
            {
                var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (attrs != null && attrs.Length > 0)
                    return ((DescriptionAttribute)attrs[0]).Description;
            }
            return str;
        }
    }





    internal class Utility
    {
        public const string RightSlash = "r";
        public const string LeftSlash = "l";
        public const string Plus = "plus";


        public static byte[] StreamToByte(Stream input)
        {
            byte[] buffer = new byte[16 * 1024];
            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }
                return ms.ToArray();
            }
        }

        public static string GetPhotoURL(int userCode)
        {
            IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
            IPAddress ipAddress = ipHostInfo.AddressList[0];
            return string.Format("http://{0}:{1}/photo.ashx?code={2}", ipAddress, ConfigurationManager.AppSettings["Application_Port"], userCode);
        }
    }

    public static class Logger
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Logger));

        public static ILog Log
        {
            get { return log; }
        }

        public static void InitLogger()
        {
            XmlConfigurator.Configure();
        }
    }

}